package com.zx.BookSeaReader.ui.login;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.BookSeaReader.R;
import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.UserService;
import com.zx.BookSeaReader.ui.home.MainActivity;
import com.zx.BookSeaReader.util.Code;
import com.zx.BookSeaReader.util.TextHelper;

public class RegisterActivity extends AppCompatActivity implements  View.OnClickListener {
    @BindView(R.id.et_registeractivity_username)
    EditText mEtRegisterUsername;
    @BindView(R.id.et_registeractivity_password1)
    EditText mEtRegisterPassword1;
    @BindView(R.id.et_registeractivity_password2)
    EditText mEtRegisterPassword2;
    @BindView(R.id.et_registeractivity_phoneCodes)
    EditText mEtRegisterPhonecodes;
    @BindView(R.id.iv_registeractivity_showCode)
    ImageView mIvRegisterShowcode;
    private String username;
    private String password1;
    private String password2;
    private String phoneCode;
    private boolean match = false;

    @BindView(R.id.iv_registeractivity_back)
    TextView mIvRegisterBack;
    @BindView(R.id.bt_registeractivity_register)
    Button mBtRegister;

    private UserService mUserService;
    private UserBean user=new UserBean();

    private String realCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mUserService=new UserService();
        ButterKnife.bind(this);


        mIvRegisterShowcode.setImageBitmap(Code.getInstance().createBitmap());
        realCode = Code.getInstance().getCode().toLowerCase();


        mIvRegisterBack.setOnClickListener(this);
        mIvRegisterShowcode.setOnClickListener(this);
        mBtRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_registeractivity_back: //返回登录页面
                Intent intent1 = new Intent(this, LoginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.iv_registeractivity_showCode:    //改变随机验证码的生成
                mIvRegisterShowcode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                break;
            case R.id.bt_registeractivity_register:    //注册按钮
                //注册验证
                username = mEtRegisterUsername.getText().toString().trim();
                password1 = mEtRegisterPassword1.getText().toString().trim();
                password2 = mEtRegisterPassword2.getText().toString().trim();
                phoneCode = mEtRegisterPhonecodes.getText().toString().toLowerCase();
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password1) && !TextUtils.isEmpty(password2) && !TextUtils.isEmpty(phoneCode) ) {

                    match =mUserService.existUser(username);

                    if(!match){
                        if(password1.equals(password2)){
                            if (phoneCode.equals(realCode)) {
                                user.setAdmin(0);
                                user.setPassword(password2);
                                user.setUsername(username);
                                //将用户名和密码加入到数据库中
                                mUserService.addUserBean(user);
                                UserBean user1 = mUserService.getUser(username);
                                Intent intent2 = new Intent(this, MainActivity.class);
                                intent2.putExtra(APPCONST.USER, user1);
                                startActivity(intent2);
                                finish();
                                TextHelper.showText("验证通过，注册成功");
                            } else {
                                TextHelper.showText("验证码错误,注册失败");
                            }
                        }else {
                            TextHelper.showText("两次输入密码不一致");
                    }
                    }else {
                        TextHelper.showText("用户已存在");
                    }
                }else {
                    TextHelper.showText("未完善信息，注册失败");
                }
                break;
        }
    }
}
